﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BusinessObjects.BO_GetObjects;
using LibraryO2S;
using BusinessObjects;



public partial class controls_admin_Messages : Pages_Base
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (divMessage.InnerHtml != "")
            divMessage.InnerHtml = "";
        if (!IsPostBack)
            loadMessages();
    }
    private int getLeftDay()
    {
        try
        {
            DataSet dsExpires = new DataSet("Expires");
            string fileName = Server.MapPath("../upload/ads/Expires.xml");
            dsExpires.ReadXml(fileName);
            if (dsExpires != null && dsExpires.Tables.Count > 0)
                return Convert.ToInt32(dsExpires.Tables[0].Rows[0][0]);
            else
                return 30;
        }
        catch
        {
            return 30;
        }
    }
    private DataTable getExpiresAds(DataTable messagesDT)
    {
        try
        {
            base.OpenConnection();
            int left_Day = getLeftDay();
            DataTable objDT = BO_Ads_Picture.Expires(left_Day);
            if (objDT != null && objDT.Rows.Count > 0)
            {
                DataRow drow = messagesDT.NewRow();
                drow["Category"] = "QuangCao";
                drow["Title"] = "Có " + objDT.Rows.Count + " quảng cáo sắp hết hạn .";
                drow["Redirect"] = "Default.aspx?module=ADSExpires&l_day=" + left_Day;
                messagesDT.Rows.Add(drow);
            }
            return messagesDT;
        }
        catch
        {
            return null;
        }
        finally { base.CloseConnection(); }
    }
    private DataTable getMessages()
    {
        DataTable messagesDT = new DataTable();
        messagesDT.Columns.Add("Category");
        messagesDT.Columns.Add("Title");
        messagesDT.Columns.Add("Redirect");
        messagesDT = getExpiresAds(messagesDT);
        return messagesDT;
    }
    private void loadMessages()
    {
        try
        {
            DataTable objDT = getMessages();
            if (objDT != null && objDT.Rows.Count > 0)
            {
                rptMessages.Visible = true;
                rptMessages.DataSource = objDT;
                rptMessages.DataBind();
            }
            else
            {
                rptMessages.Visible = false;
                rptMessages.DataSource = null;
                rptMessages.DataBind();
                divMessage.InnerHtml = O2S_Message.Warning("Cảnh Báo", "Bạn không có tin nhắn mới nào !");
            }
        }
        catch (Exception ex)
        {
            //throw ex;
            divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Phát hiện lỗi trong quá trình xử lý<br />" + ex.Message + ex.Source);
        }
    }
}
